import { safeGet } from "../utils";
import { MIN_ZOOM, MAX_ZOOM, ZOOM_STEP } from "../constant";
import { getSelectedNodes } from "../utils/flowChartUtils";
import { history } from "../history/index.js";
import { getModel } from "../state/model.js";
export var shortcuts = {
  save: {
    keys: ['meta + s', 'ctrl + s'],
    handler: function handler() {
      console.log('save');
      console.log(getModel()?.toJSON());
      return false;
    }
  },
  undo: {
    keys: ['meta + z', 'ctrl + z'],
    handler: function handler() {
      history.undo();
      return false;
    }
  },
  redo: {
    keys: ['meta + shift + z', 'ctrl + shift + z'],
    handler: function handler() {
      history.redo();
      return false;
    }
  },
  zoomIn: {
    keys: ['meta + shift + +', 'ctrl + shift + +'],
    handler: function handler(flowGraph) {
      var nextZoom = (flowGraph.zoom() + ZOOM_STEP).toPrecision(2);
      flowGraph.zoomTo(Number(nextZoom), {
        maxScale: MAX_ZOOM
      });
      return false;
    }
  },
  zoomOut: {
    keys: ['meta + shift + -', 'ctrl + shift + -'],
    handler: function handler(flowGraph) {
      var nextZoom = (flowGraph.zoom() - ZOOM_STEP).toPrecision(2);
      flowGraph.zoomTo(Number(nextZoom), {
        minScale: MIN_ZOOM
      });
      return false;
    }
  },
  copy: {
    keys: ['meta + c', 'ctrl + c'],
    handler: function handler(flowGraph) {
      var cells = flowGraph.getSelectedCells();
      if (cells.length > 0) {
        flowGraph.copy(cells);
        console.info('复制成功');
      }
      return false;
    }
  },
  paste: {
    keys: ['meta + v', 'ctrl + v'],
    handler: function handler(flowGraph) {
      if (!flowGraph.isClipboardEmpty()) {
        var cells = flowGraph.paste({
          offset: 32
        });
        flowGraph.cleanSelection();
        flowGraph.select(cells);
      }
      return false;
    }
  },
  delete: {
    keys: ['backspace', 'del'],
    handler: async function handler(flowGraph) {
      var _Modal;
      var toDelCells = flowGraph.getSelectedCells().filter(function (cell) {
        return cell.isNode();
      });
      if (toDelCells.length) {
        var Modal = (await import('ant-design-vue')).Modal;
        Modal.confirm({
          title: '确认要删除选中的节点？',
          content: '点击确认按钮进行删除，点击取消按钮返回',
          onOk: function onOk() {
            toDelCells.forEach(function (node) {
              var _model$remove;
              var _ref = node.getData() || {},
                model = _ref.model;
              model === null || model === void 0 || (_model$remove = model.remove) === null || _model$remove === void 0 || _model$remove.call(model);
            });
            history.push();
          }
        });
      }
      return false;
    }
  },
  selectAll: {
    keys: ['meta + a', 'ctrl + a'],
    handler: function handler(flowGraph) {
      flowGraph.select(flowGraph.getCells());
      return false;
    }
  },
  bold: {
    keys: ['meta + b', 'ctrl + b'],
    handler: function handler(flowGraph) {
      var cells = flowGraph.getSelectedCells();
      if (cells.length > 0) {
        var isAlreadyBold = safeGet(cells, '0.attrs.label.fontWeight', 'normal') === 'bold';
        cells.forEach(function (cell) {
          cell.setAttrs({
            label: {
              fontWeight: isAlreadyBold ? 'normal' : 'bold'
            }
          });
        });
        flowGraph.trigger('toolBar:forceUpdate');
      }
      return false;
    }
  },
  italic: {
    keys: ['meta + i', 'ctrl + i'],
    handler: function handler(flowGraph) {
      var cells = flowGraph.getSelectedCells();
      if (cells.length > 0) {
        var isAlreadyItalic = safeGet(cells, '0.attrs.label.fontStyle', 'normal') === 'italic';
        cells.forEach(function (cell) {
          cell.setAttrs({
            label: {
              fontStyle: isAlreadyItalic ? 'normal' : 'italic'
            }
          });
        });
        flowGraph.trigger('toolBar:forceUpdate');
      }
      return false;
    }
  },
  underline: {
    keys: ['meta + u', 'ctrl + u'],
    handler: function handler(flowGraph) {
      var cells = flowGraph.getSelectedCells();
      if (cells.length > 0) {
        var isAlreadyUnderline = safeGet(cells, '0.attrs.label.textDecoration', 'none') === 'underline';
        cells.forEach(function (cell) {
          cell.setAttrs({
            label: {
              textDecoration: isAlreadyUnderline ? 'none' : 'underline'
            }
          });
        });
        flowGraph.trigger('toolBar:forceUpdate');
      }
      return false;
    }
  },
  bringToTop: {
    keys: ['meta + ]', 'ctrl + ]'],
    handler: function handler(flowGraph) {
      getSelectedNodes(flowGraph).forEach(function (node) {
        return node.toFront();
      });
    }
  },
  bringToBack: {
    keys: ['meta + [', 'ctrl + ['],
    handler: function handler(flowGraph) {
      getSelectedNodes(flowGraph).forEach(function (node) {
        return node.toBack();
      });
    }
  }
};
var registerShortcuts = function registerShortcuts(flowGraph) {
  Object.values(shortcuts).forEach(function (shortcut) {
    var keys = shortcut.keys,
      handler = shortcut.handler;
    flowGraph.bindKey(keys, function () {
      return handler(flowGraph);
    });
  });
};
export default registerShortcuts;